<template>
  <div style="padding: 10px">
    <!--    功能区-->
    <div style="margin: 10px 0">
      <el-button type="primary" @click="add">新增</el-button>
    </div>
    <!--    搜索区-->
    <div style="margin: 10px 0">
      <el-input v-model="search" placeholder="输入关键字" clearable style="width: 20%" />
      <el-button type="primary" style="margin-left: 5px" @click="search_name">查询</el-button>
    </div>
    <!--    表格-->
    <el-table :data="tableData" stripe style="width: 100%">
      <el-table-column prop="id" label="ID" />
      <el-table-column prop="title" label="业务名称" />
      <el-table-column prop="author" label="业务负责人" />
      <el-table-column prop="dataTime" label="业务开始时间" />
      <el-table-column fixed="right" label="操作" >
        <template #default="scope">
          <el-button size="small" @click="handleDetail(scope.row)">详情</el-button>
          <el-button size="small" @click="handleEdit(scope.row)">编辑</el-button>
          <el-popconfirm title="确定删除吗？" @confirm="handleDelete(scope.row.id)">
            <template #reference>
              <el-button size="small" type="danger">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!--    分页-->
    <div style="margin: 10px 0 ">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-size="pageSize"
          :page-sizes="[5, 10, 20]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
      />
    </div>
    <!--    新增弹窗-->
    <div>
      <el-dialog v-model="dialogTableVisible" title="新增业务管理" width="60%">
        <el-form :model="form" label-width="120px">
          <el-form-item label="名称">
            <el-input v-model="form.title" style="width: 50%" />
          </el-form-item>
          <div id="editor—wrapper">
<!--            工具栏-->
            <div id="toolbar-container"></div>
<!--            编辑框-->
            <div id="editor-container"></div>
          </div>
        </el-form>
        <template #footer>
          <span class="dialog-footer">
            <el-button @click="dialogTableVisible = false">取 消</el-button>
            <el-button type="primary" @click="save">确 定</el-button>
          </span>
        </template>

      </el-dialog>
    </div>

<!--    详情业务内容弹窗-->
    <div>
      <el-dialog v-model="visible" title="业务详情" width="60%">
        <el-card>
          <div v-html="detail.content" style="min-height: 200px"></div>
        </el-card>
      </el-dialog>
    </div>

  </div>
</template>

<script>

import '@wangeditor/editor/dist/css/style.css'
import { createEditor, createToolbar } from '@wangeditor/editor'
import request from "@/utils/request.js";
import {ElMessage} from "element-plus";

let editor;
export default {
  name: 'Service',
  components: {

  },
  data() {
    return{
      visible:false,
      form:{},
      dialogTableVisible: false,
      search:'',
      currentPage: 1,
      pageSize:10,
      total: 0,
      tableData:[],
      detail:{},
    }
  },
  created() { //页面加载 调用
    this.load()
  },
  methods: {
    handleDetail(row) { //详情页面 弹窗
      this.visible = true
      this.detail = row
    },
    search_name() {//关键字查询
      request.get("/serviceData/search",{
        params:{
          name:this.search
        }
      }).then(res => {
        this.total = res.data.length
        this.tableData = res.data
      })
    },
    load() {
      // 分页查询
      request.get("/serviceData/limit",{
        params:{
          startIndex:this.currentPage,
          pageSize: this.pageSize
        }
      }).then(res => {
        this.tableData = res.data
      })
      //查询总条数
      request.get("/serviceData/select",{}).then(res => {
        this.total = res.data
      })
    },

    //创建编辑器的方法
    editor_create( neirong ) {
      this.$nextTick(()=> { //执行延迟回调 ,避免异步找不到dom
        const editorConfig = {
          placeholder: '输入内容',
        }
        // 创建编辑器
        editor = createEditor({
          selector: '#editor-container',
          config: editorConfig,
        })
        editor.setHtml( neirong )
        // 创建工具栏
        const toolbar = createToolbar({
          editor,
          selector: '#toolbar-container'
        })
      })
    },

    add() { //打开 新增数据 弹窗
      this.dialogTableVisible = true
      this.form = {}

      if (!editor) { // 避免 重复创建相同id 的 编辑器
        this.editor_create("")
      }else {
        editor.destroy()
        this.editor_create("")
      }
    },
    save() {//保存数据到 后台
      this.form.content = editor.getHtml() // 保存编辑器的详细内容

      if (this.form.id) {// 有id 更新数据
        request.put("/serviceData/update", this.form).then(res => {
          if (res.code == 200) {
            ElMessage({
              message: '更新成功.',
              type: 'success',
            })
          } else {
            ElMessage.error('更新失败.')
          }
          this.load()
          this.dialogTableVisible = false
        })
      }else {//没有id 新增
        //获取当前登录用户信息 赋给 业务复制人
        let userStr = sessionStorage.getItem("user") || ""
        this.form.author = userStr

        request.post("/serviceData/save", this.form).then(res => {
          if (res.code == 200) {
            ElMessage({
              message: '新增成功.',
              type: 'success',
            })
          } else {
            ElMessage.error('新增失败.')
          }
          this.load()
          this.dialogTableVisible = false // 隐藏窗口
        })
      }
    },
    handleEdit(row) { //编辑表格信息
      this.form = JSON.parse(JSON.stringify(row))
      this.dialogTableVisible = true

      if (!editor) { // 避免 重复创建相同id 的 编辑器
        this.editor_create(row.content)

      }else {
        editor.destroy()
        this.editor_create(row.content)
      }
    },
    handleDelete(id) { // 删除表格数据
      request.delete("/serviceData/delete/"+id).then(res => {
        if (res.code == 200) {
          ElMessage({
            message: '删除成功.',
            type: 'success',
          })
        } else {
          ElMessage.error('删除失败.')
        }
        this.load()//刷新表格
      })
    },
    handleSizeChange(pageSize) {//设置 每页 的 条数
      this.pageSize = pageSize
      this.load()
    },
    handleCurrentChange(currentPage) {// 设置 查询 页码
      this.currentPage = currentPage
      this.load()
    },
  }

}
</script>
